<?php
class doc extends base{

  function __construct()
  {
    parent::__construct();
    $this->m = load('m/doc');
    $this->submenu = array('mkdir'=>'创建目录','add'=>'添加文件');
  }

  function index($id)
  {
    $where = "";
    $tot = $this->m->count();
    $psize = 30;
    $param['pagination'] = pagination($tot , seg(3) , $psize ,'/doc/index/');
    $param['records'] = $this->m->get("", seg(3), $psize);
    $this->display('doc/list-table',$param);
  }


  function view($id)
  {
    $param['r'] = $this->m->get($id);
    $this->display('doc/show',$param);
  }


  function add()
  {
    $conf = array('title'=>'required');//,'uname'=>'required','input_time'=>'required','modify_time'=>'required','title'=>'required','content'=>'required','share'=>'required','share_to'=>'required','doct_type'=>'required','up_elem'=>'required',);
    
    $err = validate($conf);
    if ( $err === TRUE) {
      $arr = array(
        'title' => $_POST['title'],
        'doct_type'=>'file',
        'uid'=>$this->uid,'uname'=>$this->uname,
        'input_time'=>time(),
        'modify_time'=>time(),
      );
      $this->m->add($arr);
      redirect('/doc/add','发布成功！');
    }
    else {
      $param['val'] = $_POST; 
      $param['err'] = $err;
      $this->display('doc/add',$param);    
    }
  }    


  function mkdir()
  {
    $conf = array('title'=>'required');
    //,'share'=>'required','share_to'=>'required','doct_type'=>'required','up_elem'=>'required',);
    //'uid'=>'required','uname'=>'required','input_time'=>'required','modify_time'=>'required',
    $err = validate($conf);
    if ( $err === TRUE) {
      $arr = array(
        'title' => $_POST['title'],
        'doct_type'=>'dir',
        'uid'=>$this->uid,'uname'=>$this->uname,
        'input_time'=>time(),
        'modify_time'=>time(),
      );
      $this->m->add($arr);
      redirect('/doc/','发布成功！');
    }
    else {
      $param['val'] = $_POST; 
      $param['err'] = $err;
      $this->display('doc/mkdir',$param);    
    }
  }    

  function edit($id)
  {
    $conf = array('uid'=>'required','uname'=>'required','input_time'=>'required','modify_time'=>'required','title'=>'required','content'=>'required','share'=>'required','share_to'=>'required','doct_type'=>'required','up_elem'=>'required',);
    $err = validate($conf);
  	if ( $err === TRUE) {
      $this->m->update($id);
      redirect('/doc','修改成功！');
    }
    else {
      $param['val'] = array_merge($_POST,$this->m->get($id));
      $param['err'] = $err;
      $this->display('doc/add',$param);    
    }
  }

  function del($id)
  {
    $this->m->del($id);
    redirect('/doc/','删除成功！');
  }
}
